package com.yanyuan.algorithm.queue;


/**
 * @Description 可以在队列里存储任意元素
 * @Author yanyuan
 * @Date 10:55 2020/5/8
 * @Version 1.0
 **/
public class SqQueue<T> {
    //1.队列的成员变量
    private T[] datas;//数组模式队列实现
    private int maxSize;
    private int front;//队头指针
    private int rear;//队尾指针

    public SqQueue(int maxSize) {
        if(maxSize < 1) maxSize = 1;
        this.maxSize = maxSize;
        this.datas = (T[]) new Object[this.maxSize];//数组匹配
    }

    //队满方法
    public boolean isFull(){
        return this.front == (this.rear + 1) % maxSize;
    }

    //队空方法
    public boolean isEmpty(){
        return this.front == this.rear;
    }

    //入队操作
    public boolean put(T data){
        //队满了无法入队
        if(isFull()) return false;
        datas[rear] = data;
        rear = (rear + 1) % maxSize;//队尾指针加一
        return true;
    }

    /**
     * 出队操作
     * @return
     */
    public T take(){
        if(isEmpty()) return null;
        T data = datas[front];//出队
        front = (front + 1)% maxSize;//队头指针加一
        return data;
    }

    public T[] getDatas() {
        return datas;
    }

    public void setDatas(T[] datas) {
        this.datas = datas;
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getFront() {
        return front;
    }

    public void setFront(int front) {
        this.front = front;
    }

    public int getRear() {
        return rear;
    }

    public void setRear(int rear) {
        this.rear = rear;
    }
}
